function showcanvas(oc1, picbut, picture, src) {
    let gd1 = oc1.getContext('2d');
    let img1 = new Image();
    img1.src = src;
    gd1.drawImage(img1, 0, 0, oc1.width, oc1.height)
    picbut.click(() => {
        picture.click()
    })
    showupload(picture, oc1)
}
function showupload(picture, oc1) {
    picture.change(function () {
        let reader = new FileReader();
        reader.readAsDataURL(picture[0].files[0]);
        reader.onload = function (e) {
            let oImg = new Image();
            oImg.src = e.target.result;
            oImg.onload = function () {
                oc1.style.display = 'block';
                let gd = oc1.getContext('2d');
                gd.drawImage(oImg, 0, 0, oc1.width, oc1.height);
            }
        }
    })

}
function myuplod(oc, myid, op, num) {
    oc.toBlob(function (blob) {
        var file = new File([blob], 'aaa.png');
        const formdata = new FormData();
        formdata.append('file', file)
        $.ajax({
            url: '/upload',
            method: 'post',
            data: formdata,
            contentType: false,
            processData: false,
            success: function (res) {
                $.ajax({
                    url: '/plant/edit',
                    method: 'post',
                    data: {
                        sql: `UPDATE goods SET ${op}="../images/upload/${res.msg}" WHERE g_id=${myid}`,
                    },
                    success: function (err) {
                        if (num == 1) {
                            showpage()
                        } if (num == 0) {
                            window.location = './plant.html'
                        } if (num == 2) {
                            window.location = './farming.html'
                        }
                    },
                    error: function (err) {
                        console.log(err)
                    }
                })
            }
        })
    }, 'image/png', 0.5)
}
let maxpage
function showbut(total, count) {
    maxpage = Math.ceil(total / count)
    $('#mypage').html('')
    $('#mypage').append(`
        <button class='pre'><</button>
    `)
    for (let i = 1; i <= maxpage; i++) {
        if (page == i) {
            $('#mypage').append(`
                <button class='nowpage pages'>${i}</button>
            `)
        } else {
            $('#mypage').append(`
                <button class='pages'>${i}</button>
            `)
        }
    }
    $('#mypage').append(`
        <button class='next'>></button> 
    `)

}
$('#mypage').on('click', '.pages', function () {
    console.log(123)
    page = $(this).text()
    showpage()
})
$('#mypage').on('click', '.pre', function () {
    if (page > 1) {
        page--
        showpage()
    }
})
$('#mypage').on('click', '.next', function () {
    if (page < maxpage) {
        page++
        showpage()
    }
})
$('#showpagenum').change(() => {
    page = 1
    count = $("#showpagenum").val()
    showpage()
})
$("#gopagenum").focus(() => {
    document.onkeydown = () => {
        page = $("#gopagenum").val()
        showpage()
    }
})
function mydel(a, t, tid) {
    myid = $(a).attr('data-id')
    $('#motaikuang').html('')
    $('#motaikuang').html(`
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">是否删除</h4>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">否</button>
                    <button type="button" class="btn btn-primary delyes" data-dismiss="modal">是</button>
                </div>
            </div>
        </div>
    </div>
        `)
    $("#motaikuang").on('click', '.delyes', function () {
        $.ajax({
            url: '/plant/del',
            data: {
                id: myid, t, tid
            },
            success: function (res) {
                if (res.error == 1) {
                    alert('该商品已经进入流通环节，无法删除')
                } else {
                    if ($("#mytbody").children().length == 1) {
                        page--
                    }
                    showpage()
                }
            },
            error: function (err) {
                console.log(err)

            }
        })
    })
}